/*------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module Amount.c - Amount related functionality |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Tran/amount.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/

#define VSAMOUNT_C

#include <aclconio.h>
#include <stdio.h>
#include <applidl.h>
#include <message.h>
#include <aclstr.h>
#include <string.h>
#include <ctype.h>

#include <project.h>

/** /#define DEBUG_VSAMOUNT*/
#ifdef DEBUG_VSAMOUNT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <amount.h>
#include <gds.h>
#include <msg.h>
#include <msgfuncs.h>
#include <bdt.h>
#include <cst.h>
#include <hdt.h>
#include <cdt.h>
#include <tct.h>
#include <ui.h>
#include <sizes.h>
#include <spdebug.h>
#include <init.h>
#include <logo.h>
#include <entry.h>
#include <format.h>
#include <codesec.h>

/*-------------------------------------------------------------------------
    Function    :   inGetAmount()
    Description :   get an amount from the user.  It also gets the currency
                    if asked for.
    Parameters  :   szAmount: string to store amount.
                    fAskforCurrency: 0: don't do currency ; 1: do currency.
  Returns     :     VS_SUCCESS: amount entered.
                    VS_ERROR:   CLEAR pressed.
    Modification History :
    #     Date      Who                     Description
  ----- -------- ---------- ---------------------------------------------
    1   09/12/95 Ana_C1     Created.
    2   09/18/95 T_Al_G     Terminal independent.
    3   09/25/95 T_Al_G     Improved error-handling.
    4   10/16/95 T_Al_G     Now uses inEnterNumber() instead of SVC_KEY_NUM().
    5   10/20/95 Ana_C1     Added min and max to check the amount. Not
                            calling vdFormatAmtScreen any more. Formatting
                            happens in inEnterNumber().
    6   11/10/95 T_Al_G     Chucked an unused variable. (SEE YA!)
    7   08/06/96 Pat_G1     Remove Out of Range error message after it is
                            displayed.
 -------------------------------------------------------------------------*/

int inGetAmount(char* szAmount,VS_BOOL fAskForCurrency,long lnMin,long lnMax,char* szDisplaySymbol) {
    int x1,
    y1,
    x2,
    y2;
    int inResult;
    int inRowLim;
    char* szAmtFormat = szGetAmountFormat();
    char szCurSym[CUR_SYMBOL_SIZE + 1];
    int inDigitsInAmt = strlen(szAmtFormat);
    int inXPos;
    int inDigitCount,
    n,
    max;
    int inInputLine = obDisp.inInputLine;
    WIN_INFO wiCur8x21Win;

    /** /pdebug(("--inGetAmount--"));*/

    // SDH
    if (fAskForCurrency)
        strcpy(szCurSym, szGetCurSymbol());
    else
        szCurSym[0] = '\0';
    for (inDigitCount = 0, n = 0, max = strlen(szAmtFormat); n < max; n++) {
        if (toupper(szAmtFormat[n]) == 'N')
            inDigitCount++;
    }

    vdSetEntryRow(&inInputLine);
    inRowLim = inGetRowLim();   
    if (!obDisp.blActionFontASC8x21)
        vdCalculateCoord(GRID_4x8_TO_8x21, &inRowLim, &inInputLine);
    // SDH
    inXPos = inRowLim - inDigitsInAmt + 1;

    wherewin(&x1, &y1, &x2, &y2);

    /* Setup entry window */
    wiCur8x21Win.inLeft = inXPos;
    wiCur8x21Win.inTop = inInputLine;
    wiCur8x21Win.inRight = inRowLim;
    wiCur8x21Win.inBottom = inInputLine;
    do {
        if (obDisp.blActionFontASC8x21)
            window(wiCur8x21Win.inLeft, wiCur8x21Win.inTop, wiCur8x21Win.inRight, wiCur8x21Win.inBottom);
        inResult = inEnterNumber(&wiCur8x21Win, szAmount, inDigitCount, szCurSym, szAmtFormat);
        if (obDisp.blActionFontASC8x21)
            window(obDisp.inHomeCol, obDisp.inTitleLine, inRowLim, obDisp.inCharsPerColumn);
        if (inResult < 0) {
            //vdResetEntryFont (VS_FALSE);
            return(inResult);
        }
        vdPurgeStrofChars(szAmount, ".,");
        if ((str2long(szAmount) >= lnMin) && (str2long(szAmount) <= lnMax))
            break;

        vdDisplayMessageFromFile(OUT_OF_RANGE_ERR_MSG, WARNING_MSG);
        vdDisplayMessageFromFile(EMPTY_STR_ERR_MSG, STATUS_MSG);
        memset(szAmount, 0, sizeof(szAmount));
    } while (1);

    window(x1, y1, x2, y2);
    //vdResetEntryFont (VS_FALSE);
    return (VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function : vdComputeTransactionTotal
  Description : Computes the sum of the BaseTransactionAmount + all of
                the modifiers.
  Parameters :  char * - Amount String
  Returns :     void
    Globals :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
    1   07/19/95 Eddy_B1   Changed name of vdAddAmt() to
                           vdComputeTransactionTotal().
                           Also, the summation includes all modifiers not
                           just the tip.  Reminder to ALL there is a
                           szOtherAmount that once we figure out how to use
                           may be incorporated into this function.
    2   08/18/95 Ana_C1    Added Tax Amount
    3   06/12/96 Helen_D1  Added SurCharge Amount and % Overage
    4   07/31/96  Page_K1    Change inGetCDTType to inGGTranCardType
    5   01/30/97 Page_K1     Change %ld to SUBSTITUTE_LD     
    6   04/09/98 Kevin_V1  Back out Tax amount if Purchase card and tax not percentage.
    7   01/25/99 Page_K1   Do not back out Tax if Purchase card and fCloseTabSameCard or
                            fCloseTabNewCard
    8   02/17/98 Betty_B1  Do not back out Tax if this is a Tip Adjust or
                           an Other Adjust.  DFW 177
--------------------------------------------------------------------------*/

void vdComputeTransactionTotal(char* szAmt,TRANSACTION_OBJECT* pobTran) {
    long lnBaseAmount,
    lnCashBack,
    lnTaxAmount = 0L,
    lnTipAmount = 0L;
    long lnSurChargeAmount = 0L;

    //pdebug (("--vdComputeTransactionTotal--"));
    lnBaseAmount = pobTran->srBRec.lnBaseTransactionAmount;
    //pdebug (("Base = %ld", lnBaseAmount));

    if (pobTran->srBRec.fTaxEntered) {
        lnTaxAmount = pobTran->srBRec.lnTaxAmount;
        /*
         *      Back out Tax amount if Purchase card and tax not percentage.
         */
        if (pobTran->srBRec.inTranCardType == PURCHASE_CARD && fGetTaxProc() == VS_FALSE &&  /*lnGetTaxPercent() <= 0  && */ 
            //                (!pobTran->srBRec.fCloseTabSameCard) &&
            (!(pobTran->fTaxAdjust || pobTran->fOtherAdjust || pobTran->fTipAdjust))) {
            lnBaseAmount -= lnTaxAmount;
            pobTran->srBRec.lnBaseTransactionAmount = lnBaseAmount;
        }
    }

    //pdebug (("Tax = %ld", lnTaxAmount));

    //pdebug (("1) Tip = %ld", lnTipAmount));

    if (pobTran->srBRec.fTipEntered) {
        lnTipAmount = pobTran->srBRec.lnTipAmount;
        //pdebug (("2) Tip = %ld", lnTipAmount));
    }

    lnCashBack = pobTran->srBRec.lnCashBackAmount;
    //pdebug (("Cash Back = %ld", lnCashBack));

    if (pobTran->srBRec.lnSChgAmount > 0L) {
        lnSurChargeAmount = pobTran->srBRec.lnSChgAmount;
        //pdebug (("Surcharge = %ld",lnSurChargeAmount));
    }

    sprintf(szAmt, SUBSTITUTE_LD, (lnBaseAmount + lnTaxAmount + lnTipAmount + lnCashBack + lnSurChargeAmount));
    //pdebug(("total = %s",szAmt));
}

VS_BOOL fAmountLessThanFloorLimit(TRANSACTION_OBJECT* pobTran) {
    long int lnFloorLimitDollars;

    /** /pdebug(("--fAmountLessThanFloorLimit--"));*/

    /* fix from spbase2.5 44150_ATL28 floor limit amount error 09/22/99 gw */

    lnFloorLimitDollars = lnGetFloorLimitAmount();

    /* If the transaction amount is less than the floor limit,
        Set the transaction type to OFFLINE */
    if (pobTran->srBKey.lnTotalOfTransactionAmount < lnFloorLimitDollars)
        return(VS_TRUE);

    return(VS_FALSE);
}


VS_BOOL fFloorLimitGreaterThanZero(void) {
    /** /pdebug(("--fFloorLimitGreaterThanZero--"));*/

    /* If the floor limit amount is greater than zero return true */
    if (lnGetFloorLimitAmount() > 0)
        return(VS_TRUE);

    return(VS_FALSE);
}

int AMOUNT_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);

    return 1;
}

#undef VSAMOUNT_C

